package leetcode.editor.cn.q1_300.q50;

import java.util.Arrays;

/**
 * @author qidi
 * @date 2021-12-10 11:34:07
 */
class Q0059_SpiralMatrixIi {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[][] generateMatrix(int n) {
            int[][] matrix = new int[n][n];

            int rows = 0;
            int rowe = matrix.length;
            int cols = 0;
            int cole = matrix[0].length;
            int num = 0;
            while (num < Math.pow(n, 2)) {
                // 向左
                for (int i = cols; i < cole; i++) {
                    matrix[rows][i] = ++num;
                }
                rows++;
                if (rows == rowe) break;
                // 向下
                for (int i = rows; i < rowe; i++) {
                    matrix[i][cole - 1] = ++num;
                }
                cole--;
                if (cols == cole) break;
                // 向右
                for (int i = cole - 1; i >= cols; i--) {
                    matrix[rowe - 1][i] = ++num;
                }
                rowe--;
                if (rows == rowe) break;
                // 向上
                for (int i = rowe - 1; i >= rows; i--) {
                    matrix[i][cols] = ++num;
                }
                cols++;
                if (cols == cole) break;
            }

            return matrix;
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)


    public static void main(String[] args) {
        Solution solution = new Q0059_SpiralMatrixIi().new Solution();
        int[][] arr = null;
        arr = solution.generateMatrix(3);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(Arrays.toString(arr[i]));
        }
        System.out.println();

        arr = solution.generateMatrix(4);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(Arrays.toString(arr[i]));
        }
        System.out.println();
    }
}